/***************************************************************************
 创建者: 华磊
 开始时间: 2018.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写

 ***************************************************************************/
#include "palinstructionlogic.h"
#include "GeneralDefine.h"
#include "programengine.h"
#include "messagelog.h"
#include "palletcalculate.h"

PalInstructionLogic::PalInstructionLogic(int robotIdIn, RegData *regDataIn, RobotIo* ioControllerIn,
                                         ProgramEngine* programEngineIn
                                         , McControllerThread *programMcControllerIn, QString configPathIn)
{
  regData=regDataIn;
  robotId=robotIdIn;
  ioController=ioControllerIn;
  programEngine=programEngineIn;
  programMcController=programMcControllerIn;
  palCalculate=new PalletCalculate(robotIdIn,configPathIn);

}

E_PROGRAM_LOOP_RESULT PalInstructionLogic::startPallet(int programNum,PalInstruction palInstruct)
{
    qDebug()<<"pallet执行"<<palInstruct.stackNumber<<palInstruct.isStartSet;
    E_PROGRAM_LOOP_RESULT endFlag = E_PROGRAM_LOOP_RESULT_NORMAL;


    if(1 == palInstruct.isStartSet)
    {
        PlRegStruct plRegStruct;
        if(1!=regData->getPlRegister(palInstruct.stackNumber-1,plRegStruct))
        {
            addMsg(ENUM_MSG_ERROR,"ProgramEngine","ProgramEngine",12900,robotId);
            return E_PROGRAM_LOOP_RESULT_ERROR;
        }
        int tmpKey=palCalculate->palletCalculateOnce(palInstruct.stackNumber-1,plRegStruct.row,plRegStruct.col,plRegStruct.layer);
        if(1!=tmpKey)
        {
            addMsg(ENUM_MSG_ERROR,"ProgramEngine","ProgramEngine",12901,robotId);
            return E_PROGRAM_LOOP_RESULT_ERROR;
        }

        regData->setPlRegValue(palInstruct.stackNumber-1,plRegStruct);


        programEngine->updateProgramSetRegister();



    }
    else
    {

    }

    programEngine->calculateNextLineToRun(programNum);

    endFlag = programEngine->isTheLastInstruction(programNum);

    return endFlag;
}

int PalInstructionLogic::getPalletConditionInfo(std::vector<PalletMiddleInfo> &infoOut)
{
    return palCalculate->getPalletConditionInfo(infoOut);
}

int PalInstructionLogic::setPalletConditionInfo(StackInfo infoIn)
{
    return palCalculate->setPalletConditionInfo(infoIn);
}


PointPro PalInstructionLogic::parsePRData(int programNum)
{
    PointPro regPr;
    return regPr;
}

float PalInstructionLogic::parseRegisterData(int programNum)
{
    return 0;
}


QVector <int> PalInstructionLogic::parsePLData(int programNum)
{

    QVector <int> plTempList;
    return plTempList;
}


void PalInstructionLogic::addMsg(int messageLevel, string componentName, string messageType,
                           int messageCode, int robotId,
                           int parameter1, int parameter2, int parameter3, int parameter4,QString message)
{
    Message tmpMsg;
    tmpMsg.MessageLevel = messageLevel;
    tmpMsg.componentClassName = componentName;
    tmpMsg.messageType = messageType;
    tmpMsg.robotId = robotId;
    tmpMsg.messageCode = messageCode;
   // tmpMsg.time = t1.tv_sec;


    QString infomationStr;

    switch(messageCode)
    {
    case 12901:
    {
        infomationStr =  QObject::tr("机器人:")  + QString::number(robotId);
        infomationStr = infomationStr + QObject::tr(",码跺计算错误!");
    }
        break;
    case 12900:
    {
        infomationStr =  QObject::tr("机器人:")  + QString::number(robotId);
        infomationStr = infomationStr + QObject::tr(",寄存器指令错误，寄存器的范围超过限定的最大范围，请修改");
    }
        break;

    default:
    {

    }
    }


    tmpMsg.MessageInformation = infomationStr.toStdString();

    // qDebug()<<"53444444444444444444444"<< tmpMsg.messageCode;
    MessageLog::getInstance()->addMessage(tmpMsg);
}
